<template>
    <!-- 
    <b-modal ref="my-modal" hide-footer title="Using Component Methods">
     <div class="d-block text-center">
       <h3>Hello From My Modal!</h3>
     </div>
     <b-button class="mt-3" variant="outline-danger" block @click="hideModal">Close Me</b-button>
     <b-button class="mt-2" variant="outline-warning" block @click="toggleModal">Toggle Me</b-button>
   </b-modal> 
    -->
    <!--
        body-class="modalStyle" 表示样式
        size="lg" 表示模态窗口大小 xl,lg,sm,full
        hide-footer 表示隐藏底部按钮
        hide-header 表示隐藏头部内容
        no-close-on-backdrop 表示鼠标点击背景不可关闭
        hide-header-close 表示隐藏头部关闭按钮
        centered 居中
        ok-title=“确定” 
        cancel-title=取消
        @ok="handleOk" 
        @cancel="handleCancel"
        scrollable 滚动条
        hide-backdrop 隐藏背景
        wrapClassName="ant-modal-cust-warp" 
        style="top:5%;height: 85%;overflow-y: hidden" 样式
        :modal-class="my-modal"
        dialog-class="modal-dialog"
        @hidden 关闭窗体触发事件
    -->
    <b-modal ref="my-modal" :title="title" scrollable  @hidden="onHidden" no-close-on-backdrop >
        <div class="d-block text-center">
            <div class="form-group m-form__group row">
                <label class="col-lg-2 col-form-label">发起类型</label>
                <div class="col-lg-6">
                    <select class="form-control" v-model="lcStartForm.complated" onchange="changeType()">
                        <option value="10">发起流程实例并完成第一个节点</option>
                        <option value="20">发起流程实例不完成第一个节点</option>
                    </select>
                </div>
            </div>
            <div class="form-group m-form__group row">
                <label class="col-lg-2 col-form-label">标&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题</label>
                <div class="col-lg-4">
                    <input class="form-control" v-model="lcStartForm.title" maxlength="64">
                </div>
            </div>
            <div class="form-group m-form__group row">
                <label class="col-lg-2 col-form-label">业务Key</label>
                <div class="col-lg-6">
                    <input class="form-control" type="text" maxlength="100" v-model="lcStartForm.businessKey" placeholder="请输入">
                </div>
            </div>
            <div class="form-group m-form__group row">
                <label class="col-lg-2 col-form-label">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注</label>
                <div class="col-lg-8">
                    <textarea class="form-control" rows="3"  v-model="lcStartForm.remark" maxlength="800"></textarea>
                </div>
            </div>
            <div class="form-group m-form__group row">
                <label class="col-lg-2 col-form-label">处理人</label>
                <div class="col-lg-6">
                    <div class="input-group">
                        <input type="text" readonly class="form-control m-input" v-model="lcStartForm.mutilValueText" placeholder="请选择" aria-describedby="basic-addon2">
                        <div class="input-group-append">
                        <span style="cursor:pointer;" class="input-group-text" @click="resetUsersBl()">
                            <i class="fa fa-times"></i>
                        </span>
                        </div>
                        <div class="input-group-append">
                        <span  style='cursor:pointer;' class="input-group-text" @click='initUsersBl()'>
                            <i class="flaticon-user-ok"></i>
                        </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <template slot="modal-footer">
            <!--自定义按钮-->
            <button type="button" class="btn btn-light-danger font-weight-bold mr-2" block @click="hideModal">
                <i class="icon-xl fa fa-times"></i>取 消
            </button> 
            <button type="button" class="btn btn-light-success font-weight-bold mr-2" @click="doTaskAction">
                <i class="icon-xl fas fa-save"></i> 保 存
            </button>
        </template>
        <UserSelect ref="userSelectRef" :userOption="{userOption}" @change="doSetUser"></UserSelect><!--采用父窗口关闭传递的方法-->
    </b-modal>    
</template>
<script>
import apiUtil from "@/core/util/apiUtil.js";
import UserSelect from "@/components/user-select.vue";
import { handleNotify,handleAlert,handleConfirm,showWating,closeWating} from "@/core/util/jehcUtil.js";
export default {
    data(){
        return {
            userOption:{allowMultiSelect:false},
            title:"根据自定义配置发起实例",
            id:"",           
            deploymentId:"",
            lcStartForm:{
                complated:"10",
                title:"",
                businessKey:"",
                mutilValueText:"",
                remark:"",
                mutilValue:"",
                moduleKey:"",
                version:"",
            }
        }
    },
    components: {
        UserSelect,
    },
    watch: {
    },
    mounted() {        
       
    },
    methods: {        
        showModal(row) {
            this.restForm();
            this.$refs['my-modal'].show();
            if(null != row && undefined != row){
                this.id = row.id;
                this.title = "根据自定义配置发起实例【"+row.deploymentName+"】";
                this.deploymentId = row.deploymentId
                this.getDetail(row.lcProcessId,row.version)
            }            
        },
        hideModal() {
            this.restForm();
            this.$refs['my-modal'].hide()
        },
        toggleModal() {
            // 当模态已隐藏时，我们传递要返回焦点的按钮的ID
            this.$refs['my-modal'].toggle('#toggle-btn')
        },
        onHidden(){//关闭窗体触发事件
            this.$emit("change",null);
        },
        doTaskAction() {//发起业务实例（流程自带）
            if(this.lcStartForm.businessKey === ""){
                handleAlert("请输入业务Key", "warning", 3)
                return;
            }
            // 保存前提示
            // 操作前提示
            this.$bvModal.msgBoxConfirm("确定要发起实例？", {
                title: '提示',
                size: 'sm',
                buttonSize: 'sm',
                okVariant: 'success',
                okTitle: '确定',
                cancelTitle: '取消',
                headerClass: 'p-2 border-bottom-0',
                footerClass: 'p-2 border-top-0',
                hideHeaderClose: false,
                centered: true
            }).then(value => {
                if(value){
                    apiUtil.post(process.env.VUE_APP_WORKFLOW_API+"/lcProcess/module/start", this.lcStartForm).then(({ data }) => {
                        if(data.success){
                            handleAlert("操作成功", "success", 3);
                            this.hideModal();//关闭窗体
                            this.$emit("change",data);//组件传值，即向父级模态框中传递值，采用change方式，目标刷新主列表使用
                        }else {
                            handleAlert("操作失败", "error", 3);
                        }            
                    });
                }                
            }).catch(err => {               
            })               
        },
        resetUsersBl(){
            this.lcStartForm.mutilValue = "";
            this.lcStartForm.mutilValueText = "";
        },
        initUsersBl(){
            this.$refs.userSelectRef.showModal();
        },
        restForm() {
            //重置
            Object.assign(this.$data.lcStartForm, this.$options.data().lcStartForm);
        },
        doSetUser(data){
            if(data){
                let id = data.map((item) => item.id);
                let names =  data.map((item) => item.name);
                this.lcStartForm.mutilValue = id.join(",");
                this.lcStartForm.mutilValueText = names.join(",");
            }
        },
        getDetail(lc_process_id,version){            
            apiUtil.get(process.env.VUE_APP_WORKFLOW_API+"/lcProcess/get/"+lc_process_id).then(({ data }) => {
                let moduleKey = data.data.moduleKey;
                let thiz = this;                
                if(null == moduleKey|| moduleKey == ""){
                    handleAlert("未能获取到模块Key", "warning", 3)
                    return;
                }                
                thiz.lcStartForm.moduleKey = moduleKey;
                thiz.lcStartForm.version = version;
                thiz.lcStartForm.complated = "10";
                apiUtil.get(process.env.VUE_APP_WORKFLOW_API+"/lcProcess/attribute/"+moduleKey+"/"+version).then(({ data }) => {//验证流程定义中第一个任务节点发起人节点是否存在
                    if(null != data.data){
                        // thiz.lcStartForm = data.data;
                    }
                })
                // .catch(err => {    
                //     thiz.lcStartForm.complated = "10";
                // });
            }) ;
        },
   }
}
</script>